package com.xinzi.apartment.controller;

import com.xinzi.apartment.common.exception.InvalidParamException;
import com.xinzi.apartment.entity.StuInfoEntity;
import com.xinzi.apartment.service.StuInfoService;
import com.xinzi.apartment.vo.StuDistributionVo;
import com.xinzi.apartment.common.exception.BizCodeEnum;
import com.xinzi.apartment.common.exception.NoEnoughDormitoryException;
import com.xinzi.apartment.common.utils.PageUtils;
import com.xinzi.apartment.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author xinzi
 * @email 1228604898@qq.com
 * @date 2021-10-04 17:16:18
 */
@RestController
@RequestMapping("apartment/stuinfo")
@Slf4j
public class StuInfoController {
    @Autowired
    private StuInfoService stuInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("apartment:stuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = stuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("apartment:stuinfo:info")
    public R info(@PathVariable("id") Integer id){
		StuInfoEntity stuInfo = stuInfoService.getById(id);

        return R.ok().put("stuInfo", stuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("apartment:stuinfo:save")
    public R save(@RequestBody StuInfoEntity stuInfo){
		stuInfoService.save(stuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("apartment:stuinfo:update")
    public R update(@RequestBody StuInfoEntity stuInfo){
		stuInfoService.updateById(stuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("apartment:stuinfo:delete")
    public R delete(@RequestBody Integer[] ids){
		stuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @RequestMapping("/not-distribute")
    @RequiresPermissions("apartment:stuinfo:list")
    public R listNotDistribute(@RequestParam Map<String, Object> params){
        PageUtils page = stuInfoService.queryNotDistribute(params);

        return R.ok().put("page", page);
    }


    @PostMapping("/auto-distribution")
    @RequiresPermissions("apartment:stuinfo:distribution")
    public R autoDistribution(@RequestBody Integer[] ids) {
        try {
            List<StuDistributionVo> stuDistributionVos = stuInfoService.autoDistribute(ids);
            return R.ok().put("data", stuDistributionVos);
        } catch (NoEnoughDormitoryException e) {
            log.warn("No Enough Dormitory Exception detected!");
            return R.error(BizCodeEnum.NO_ENOUGH_DORMITORY.getCode(), BizCodeEnum.NO_ENOUGH_DORMITORY.getMessage());
        } catch (InvalidParamException e) {
            log.warn("Invalid Param Exception detected!");
            return R.error(BizCodeEnum.INVALID_PARAM.getCode(), BizCodeEnum.INVALID_PARAM.getMessage());
        } catch (Exception e) {
            log.warn("Unknown Error detected!");
        }
        return R.error(BizCodeEnum.UNKNOWN_EXCEPTION.getCode(), BizCodeEnum.UNKNOWN_EXCEPTION.getMessage());
    }

}
